package com.eurasia.assignment.domain;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

import com.eurasia.assignment.infrastructure.entity.FileEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 项目分配领域对象
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProjectAssignment {
    private Long id;
    private Long projectId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    
    @Builder.Default
    private Set<TeamMember> teamMembers = new HashSet<>();
    
    /**
     * 添加团队成员
     * @param userId 用户ID
     * @param role 团队角色
     */
    public void addTeamMember(Long userId, TeamRole role) {
        TeamMember member = new TeamMember();
        member.setUserId(userId);
        member.setTeamRole(role);
        teamMembers.add(member);
    }
    
    /**
     * 添加团队负责人
     * @param userId 用户ID
     */
    public void addTeamLead(Long userId) {
        addTeamMember(userId, TeamRole.TEAM_LEAD);
    }
    
    /**
     * 创建新的项目分配
     * @param projectId 项目ID
     * @return 项目分配实例
     */
    public static ProjectAssignment create(Long projectId) {
        ProjectAssignment assignment = new ProjectAssignment();
        assignment.setProjectId(projectId);
        assignment.setCreateTime(LocalDateTime.now());
        assignment.setUpdateTime(LocalDateTime.now());
        return assignment;
    }
    
    /**
     * 添加提交
     * @param description 描述
     * @param type 提交类型
     * @param submitterId 提交者ID
     * @param file 文件实体
     * @return 创建的提交
     */
    public Submission addSubmission(String description, String type, String submitterId, FileEntity file) {
        // 检查提交者是否为团队成员
        boolean isTeamMember = false;
        if (submitterId != null) {
            Long submitterIdLong = Long.parseLong(submitterId);
            isTeamMember = teamMembers.stream()
                    .anyMatch(member -> member.getUserId().equals(submitterIdLong));
        }
        
//        if (!isTeamMember) {
//            throw new IllegalArgumentException("提交者不是项目团队成员");
//        }
        
        // 创建提交
        return Submission.create(projectId, description, type, submitterId, file);
    }
} 